<template>
	<section>
		<!--工具条 - 高级查询框-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
      private Employee admin;
      private TenantType type;
			<el-table-column prop="companyName" label="租户名称" width="100" sortable>
			</el-table-column>
      <el-table-column prop="companyNum" label="租户座机" width="120" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color: darkgray">待审核</span>
          <span v-else-if="scope.row.state == 2" style="color: red">审核失败</span>
          <span v-else style="color: green">审核通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="logo" label="logo" width="100" sortable>
      </el-table-column>
      <el-table-column prop="admin.username" label="管理员" width="100" sortable>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="120"  sortable>
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条 - 批量删除&分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          :current-page="query.page"
          @current-change="handleCurrentChange"
          :page-size="query.rows"
          :total="pageInfo.total"
          style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="tenantFormVisible" :close-on-click-modal="false">
			<el-form :model="tenantForm" label-width="80px" :rules="tenantFormRules" ref="tenantForm">
				<el-form-item label="键" prop="key">
					<el-input v-model="tenantForm.key" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="tenantForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input type="textarea" v-model="tenantForm.intro" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="tenantFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="tenantFormLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				query:{
				  page:1,  // 当前页
          rows:2,  // 每页显示条数
          keyword:""  // 关键字
        },
        pageInfo:{
				  total:0,
          rows:[]
        },
				listLoading: false,
				sels: [],//列表选中列
        
				tenantFormVisible: false,//新增修改界面是否显示
				tenantFormLoading: false,
				tenantFormRules: {
					key: [
						{ required: true, message: '请输入键', trigger: 'blur' }
					],
          value: [
            { required: true, message: '请输入值', trigger: 'blur' }
          ]
				},

				//新增修改界面数据
				tenantForm: {
					id:null,
          key:"",
          value:"",
          intro:"",
				}
			}
		},
		methods: {
      search(){
        this.query.page = 1;
        this.getTenants();
      },
			handleCurrentChange(val) {  // val是当前页
				this.query.page = val;
				this.getTenants();
			},
			//获取租户类型列表
			getTenants() {
				this.listLoading = true;  // 弹出加载框
				this.$http.post("/sysmanage/tenant/list",this.query)
                  .then(result => {
                    result = result.data;
                    if(result.success){
                       this.pageInfo = result.resultObj;
                       this.listLoading = false;  // 关闭加载框
                    }else{
                      this.$message({message: '查询分页失败!'+result.message, type: 'error'});
                    }
                  })
                  .catch(result => {
                    this.$message({message: "查询分页失败!", type: 'error'});
                  })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/tenant/"+row.id)
                    .then(result => {
                       result = result.data;
                       if(result.success){
                         this.$message({message: '删除成功', type: 'success'});
                         // 刷新列表
                         this.query.page = 1;
                         this.getTenants();
                         this.listLoading = false;  // 关闭弹出框
                       }else{
                         this.$message({message: '删除失败!'+result.message, type: 'error'});
                       }
                    })
				}).catch(() => {
          this.$message({message: '已取消删除', type: 'info'});
				});
			},

			//显示编辑界面
			handleEdit: function (index, row) {
				this.tenantFormVisible = true;
				this.tenantForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.tenantFormVisible = true;
				this.tenantForm = {
          id:null,
          key:"",
          value:"",
          intro:"",
        }
			},
			//新增修改保存方法
			save: function () {
				this.$refs.tenantForm.validate((valid) => {   // 判断tenantForm表单校验是否通过,  valid = true/false  true校验通过 false 校验未通过
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.tenantFormLoading = true;
							let para = Object.assign({}, this.tenantForm);
							// 级联下拉框绑定后的值是数组,但是后端的parent需要的是一个对象,所以需要我们手动转一下
							if(para.parent != null){
							  let array = para.parent;
							  // 当用户选择多级部门以后,对于该部门而言,最后一个部门id才是它的父部门
                para.parent = {id:array[array.length-1]};
              }
							this.$http.put("/tenant",para)
                  .then(result => {
                      result = result.data;
                      if(result.success){
                        this.$message({message: '保存成功', type: 'success'});
                        this.tenantFormLoading = false;
                        this.query.page = 1;
                        this.getTenants();
                        this.tenantFormVisible = false;
                      }else{
                        this.$message({message: '保存失败!'+result.message, type: 'error'});
                      }
                  }).catch(result => {
                      this.$message({message: '保存失败!', type: 'error'});
                  })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getTenants();
					});
				}).catch(() => {

				});
			}
		},
		mounted() { 
			this.getTenants();  // 做分页查询
		}
	}

</script>

<style scoped>

</style>